package com.woniuxy.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.mapper.BaseAreaMapper;
import com.woniuxy.base.model.entity.BaseArea;
import com.woniuxy.base.model.entity.BaseAreaRecord;
import com.woniuxy.base.model.entity.BaseMaterial;
import com.woniuxy.base.service.BaseAreaRecordService;
import com.woniuxy.base.service.BaseAreaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BaseAreaServiceImpl implements BaseAreaService {
    @Resource
    private BaseAreaMapper baseAreaMapper;

    @Resource
    private BaseAreaRecordService recordService;

    @Override
    public Page<BaseArea> getAll(int current, int size) {
        return baseAreaMapper.selectPage(new Page<>(current, size),null);
    }

    @Transactional
    @Override
    public Boolean update(BaseArea baseArea, String account) {
        BaseArea record = baseAreaMapper.selectById(baseArea.getId());
        BaseAreaRecord areaRecord = new BaseAreaRecord();
        areaRecord.setArea(record.getArea())
                        .setState(record.getState())
                        .setDescription(record.getDescription()).setUpdateTime(record.getAddTime())
                        .setAreaId(record.getId()).setUpdatePerson(account);
        baseAreaMapper.update(baseArea, new UpdateWrapper<BaseArea>().eq("id",baseArea.getId()));
        recordService.add(areaRecord);
        return true;
    }

    @Override
    public Boolean add(BaseArea baseArea) {
        baseAreaMapper.insert(baseArea);
        return true;
    }

    @Override
    public Page<BaseArea> conditionSearch(BaseArea baseArea, Integer current, Integer size) {
        return baseAreaMapper.selectPage(new Page<>(current,size),new QueryWrapper<BaseArea>(baseArea));
    }

    @Override
    public Page<BaseArea> keywordSearch(String keyword, Integer current, Integer size) {
        QueryWrapper<BaseArea> wrapper = new QueryWrapper<>();
        wrapper.like("area",keyword).or().like("description",keyword);
        return baseAreaMapper.selectPage(new Page<>(current,size),wrapper);
    }

    @Override
    public List<BaseAreaRecord> detail(Integer areaId) {
        return recordService.detail(areaId);
    }

    @Override
    public List<BaseArea> baseList() {
        return baseAreaMapper.selectList(null);
    }

}
